package inits

import (
	"fmt"
	"github.com/redis/go-redis/v9"
	"go.uber.org/zap"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"video-hub/global"
	"video-hub/internal/mobel"
)

func Init() {
	ZapLiit()
	MysqlLink()
	RedisLink()

}

func ZapLiit() {
	global.Logger, _ = zap.NewDevelopment()
	//global.Logger.Log(1, "日志初始化成功")
}

func MysqlLink() {
	Dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", "root", "89B158B4DAFE6F5461878D26EA49B100", "14.103.158.217", "3306", "video_hub")
	var err error
	global.DB, err = gorm.Open(mysql.Open(Dsn))
	if err != nil {
		global.Logger.Info("log", zap.Bool("mysql link", false))
		panic(err)
	}
	global.Logger.Info("log", zap.Bool("mysql link", true))
	if err = global.DB.AutoMigrate(&mobel.User{}, &mobel.Video{}, &mobel.VideoHub{}); err != nil {
		global.Logger.Info("log", zap.Bool("mysql AutoMigrate", false))
		panic(err)
	}
	global.Logger.Info("log", zap.Bool("mysql AutoMigrate", true))
}

func RedisLink() {
	global.RDB = redis.NewClient(&redis.Options{
		Addr:     "14.103.158.217:6379",
		Password: "redis_x3jTtC",
		DB:       0,
	})
	if err := global.RDB.Set(global.Ctx, "tries", "ok", 0).Err(); err != nil {
		global.Logger.Info("log", zap.Bool("redis link", false))
		panic(err)
	}
	global.Logger.Info("log", zap.Bool("redis link", true))
}
